<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button
            v-if="$auth('/setting/delivery/template/create')"
            type="primary"
            icon="plus"
            @click="handleAdd"
          >新增</a-button>
        </a-col>
        <a-col :span="8" :offset="10">
          <a-input-search
            style="max-width: 300px; min-width: 150px;float: right;"
            v-model="queryParam.search"
            placeholder="請輸入運費範本名稱"
            @search="onSearch"
          />
        </a-col>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="delivery_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <!-- 计费方式 -->
      <span slot="method" slot-scope="text">
        <span>{{ text == 10 ? '按件數' : '按重量' }}</span>
      </span>
      <!-- 操作 -->
      <span slot="action" slot-scope="text, item">
        <a
          v-if="$auth('/setting/delivery/template/update')"
          style="margin-right: 8px;"
          @click="handleEdit(item)"
        >編輯</a>
        <a v-action:delete @click="handleDelete(item)">删除</a>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import * as Api from '@/api/setting/delivery'
import { STable } from '@/components'

export default {
  name: 'Index',
  components: {
    STable
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '範本ID',
          dataIndex: 'delivery_id'
        },
        {
          title: '範本名稱',
          dataIndex: 'name'
        },
        {
          title: '計費方式',
          dataIndex: 'method',
          scopedSlots: { customRender: 'method' }
        },
        {
          title: '排序',
          dataIndex: 'sort'
        },
        {
          title: '添加時間',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created () {

  },
  methods: {

    /**
     * 新增记录
     */
    handleAdd () {
      this.$router.push('./create')
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$router.push({ path: './update', query: { deliveryId: item.delivery_id } })
    },

    /**
     * 删除记录
     */
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: '您確定要删除該記錄嗎?',
        content: '删除後不可恢復',
        onOk () {
          return Api.deleted({ deliveryId: item['delivery_id'] })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch () {
      this.handleRefresh(true)
    }

  }
}
</script>
